from dealer import Dealer
from config import *
import ekg

class Player:
	u"Gracz"
	def __init__(self):
		self.strength = 0
		self.name = "UnnamedPlayer"
		self.uin = 0
		self.money = 1000
		self.hand = []
		self.strongest_hand = []
		self.set_name = ""
		self.table = 0
		self.allin = False
		self.blind = 0 # 1=SB, 2=BB
		self.ready = False
		self.active = True #active in bidding
		self.ingame = True #player is in game (hasn't passed)
	def __repr__(self):
		return "%s (%d)" % (self.name, self.strength)
	def UpdateStat(self, strength, set_name, set):
		if strength > self.strength:
			self.strength = strength
			self.strongest_hand = set
			self.set_name = set_name
	def SetIdentity(self, uin, name, blind):
		self.uin = uin
		self.name = name
		self.blind = blind
	def PickStrongestHand(self, cards):
		sets = Dealer().GetSets(cards + self.hand)
		for i in range(len(sets)):
			score, set_name = Dealer().ScoreSet(sets[i])
			self.UpdateStat(score, set_name, sets[i])
	def TakeCards(self, cards):
		self.hand = cards
	def IsActive(self):
		return self.active
	def Bet(self, amount, table):
		u"Place a bet on the table"
		if amount < table.stake - self.table:
			raise UserWarning, "Can't place a bet lower than opponent stake"
		if self.money >= amount and amount > 0:
			self.money -= amount
			self.table += amount
			if self.table > table.stake:
				table.ResetAllActives() #will make possible to bet/raise for other players (player.active = true)
				self.active = False
			else:
				self.active = False
			table.Raise(amount)
		elif amount == 0:
			self.active = False
		else:
			raise UserWarning, "Not sufficient money or money <= 0"
	def Call(self, table):
		u"Call"
		amount = table.stake - self.table
		self.Bet(amount, table)
	def Pass(self, table):
		u"Pass"
		self.active = False
		self.ingame = False
	def SmallBlind(self, table): #TODO: if not sufficient cash to place a blind then game over
		self.Bet(sb, table)
	def BigBlind(self, table):
		self.Bet(bb, table)
	def PayBlind(self, table):
		u"determines blind type and pays it"
		if self.blind == 1:
			self.SmallBlind(table)
		elif self.blind == 2:
			self.BigBlind(table)
	def SetName(self, name):
		self.name = name
	def SetUin(self, uin):
		self.uin = uin
	def ResetMoney(self):
		self.money = 1000
	def ResetBidActiveFlag(self):
		self.active = True
	def ResetBet(self):
		self.table = 0
	def ResetStats(self):
		self.hand = []
		self.strongest_hand = []
		self.allin = False
		self.ready = False
		self.blind = 0
		self.strength = 0
		self.ingame = True
		self.table = 0
	def ResetPlayer(self):
		self.ResetStats()
		self.ResetMoney()
		self.ResetBidActiveFlag()
	def Earn(self, amount):
		self.money += amount
	def ReadyUp(self):
		self.ready = True
	def Text(self, text):
		cmd = "/msg %s %s" % (self.uin, text)
		ekg.command(cmd)
		#Messages are sent to EKG via /msg command
		#console text is now depracated.
